package cn.disicn.controller;

import org.apache.commons.lang3.StringUtils;
import cn.disicn.common.Const;
import cn.disicn.common.ServerResponse;
import cn.disicn.model.SecPublishUser;
import com.jfinal.core.Controller;

/**
 * 
 * @author zjk
 * @date 2017年12月21日 下午2:38:07
 * @desc pc用户管理
 * 
 */
public class SecPublishUserController extends Controller {

	private static final SecPublishUser secPublishUser = new SecPublishUser();

	/**
	 * 
	 * @desc 查询所有用户集合
	 */
	public void index() {
		Integer pageNum = getParaToInt("pageNum");
		Integer pageSize = getParaToInt("pageSize");
		String status = getPara("status");
		if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
			renderJson(secPublishUser.getSecUserList(pageNum, pageSize, status));
			return;
		}
		renderJson(ServerResponse.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}

	/**
	 * 
	 * @desc 冻结账号
	 */
	public void updateStatus() {
		String id = getPara("id");
		String type = getPara("type");
		if (!StringUtils.isBlank(id) && !StringUtils.isBlank(type) && id.length() == 32) {
			Integer code = Const.UserStatusInfoEnum.statusValueOf(type).getCode();
			if (code != null) {
				if (code == Const.UserStatusInfoEnum.FROZEN_BIZ_JOBS.getCode()) {
					secPublishUser.setStatus(Const.UserStatusInfoEnum.FROZEN_BIZ_JOBS.getCode() + "");
				} else if (code == Const.UserStatusInfoEnum.THAW_BIZ_JOBS.getCode()) {
					ServerResponse<String> response = secPublishUser.getAuditStatus(id);
					if (response != null) {
						if (response.isSuccess()) {
							secPublishUser.setStatus(Const.UserStatusEnum.AUTHENTICATED.getCode() + "");
						} else {
							secPublishUser.setStatus(Const.UserStatusEnum.NOT_CERTIFIED.getCode() + "");
						}
					}
				}
				secPublishUser.setId(id);
				renderJson(secPublishUser.updateStatus(secPublishUser));
				return;
			}
		}
		renderJson(ServerResponse.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}

	/**
	 * 
	 * @desc 判断是否实名认证
	 */
	public void judgeWhetherRealName() {
		String id = getPara("id");
		if (!StringUtils.isBlank(id) && id.length() == 32) {
			if (secPublishUser.getRealNameAuthInfo(id).getData() == null) {
				renderJson(ServerResponse.createByErrorMsg("该用户没有提交实名认证信息"));
			} else {
				renderJson(ServerResponse.createBySuccess());
			}
			return;
		}
		renderJson(ServerResponse.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}

	/**
	 * 
	 * @desc 修改实名认证状态
	 */
	public void auditRealName() {
		String id = getPara("id");
		String text = getPara("text");
		String audit = getPara("audit");
		String type = getPara("type");
		if (!StringUtils.isBlank(id) && !StringUtils.isBlank(text) && !StringUtils.isBlank(audit) && id.length() == 32
				&& Const.AuditStatusEnum.codeOf(Integer.parseInt(audit)).getValue() != null) {
			Integer auditstatus = Integer.parseInt(audit);
			secPublishUser.setId(id);
			secPublishUser.setAuditStatus(Integer.parseInt(audit));
			secPublishUser.setAuditResult(text);
			if (Const.AuditStatusEnum.SUCCESSFUL_AUDIT.getCode() == auditstatus) {
				if (!StringUtils.isBlank(type)) {
					secPublishUser.setType(Integer.parseInt(type));
				}
				secPublishUser.setStatus(Const.UserStatusEnum.AUTHENTICATED.getCode() + "");
			} else if (Const.AuditStatusEnum.AUDIT_FAILURE.getCode() == auditstatus) {
				secPublishUser.setStatus(Const.UserStatusEnum.NOT_CERTIFIED.getCode() + "");
			}
			renderJson(secPublishUser.updateAuditStatus(secPublishUser));
			return;
		}
		renderJson(ServerResponse.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}
}
